package com.sjzl.service.impl;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.sjzl.entity.Notification;
import com.sjzl.entity.User;
import com.sjzl.service.NotificationService;
import com.sjzl.service.UserService;

@Component("notificationServiceImpl")
public class NotificationServiceImpl extends ServiceImpl<Notification> implements NotificationService{
	
	@Resource(name="userServiceImpl")
	private UserService userService;

	@Override
	public void create(Notification notification) {}

	@Transactional
	public boolean create(Notification notification, String userAccount) {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession httpSession = request.getSession();
		User sessionUser = (User) httpSession.getAttribute("sessionUser");
		
		User user = userService.getUserByAccount(userAccount);
		notification.setDateCreated(new Date());
		notification.setUser(user);
		notification.setIsNew(1);
		notification.setUserCreated(sessionUser.getAccount());
		this.getDao().save(notification);
		return true;
	}

	@Override
	@Transactional
	public void delete(Notification notification) {
		notification.setUser(null);
		super.delete(notification);
	}

}
